using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Tino.Controls
{
    public class ReadOnlyRichTextBox : RichTextBox, IRtfWritter, IMessageFilter
    {
        int WM_MOUSEWHEEL = 0x20A;
        bool hasFocus = false;

        [DllImport("user32.dll")]
        static extern bool GetCursorPos(ref Point lpPoint);
        
        [DllImport("User32.dll")]
        static extern Int32 SendMessage(int hWnd, int Msg, int wParam, int lParam);

        public ReadOnlyRichTextBox() : base()
        {
            GotFocus += delegate
            {
                hasFocus = true;
            };

            LostFocus += delegate
            {
                hasFocus = false;
            };
        }

        #region IRtfWritter Members

        public string RtfText
        {
            get
            {
                return Rtf;
            }
            set
            {
                Rtf = value;
            }
        }

        #endregion

        #region IMessageFilter Members

        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == WM_MOUSEWHEEL && !hasFocus)// && VerticalScroll.Visible)
            {
                Focus();
                SendMessage((int)Handle, m.Msg, (Int32)m.WParam, (Int32)m.LParam);
                return true;
            }
            return false;
        }

        #endregion
    }
}
